<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-AAS.
 *
 * PHP-AAS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-AAS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-AAS; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-aas
 * @subpackage  deploy
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

/**
 * Representation of a web resource collection for a web application's security
 * constraint, as represented in a <code>&lt;web-resource-collection&gt;</code>
 * element in the deployment descriptor.
 * <p>
 * <b>WARNING</b>:  It is assumed that instances of this class will be created
 * and modified only within the context of a single thread, before the instance
 * is made visible to the remainder of the application.  After that, only read
 * access is expected.  Therefore, none of the read and write access within
 * this class is synchronized.
 */

final class SecurityCollection {


    // ----------------------------------------------------------- Constructors

    /**
     * Construct a new security collection instance with specified values.
     *
     * @param name Name of this security collection
     * @param description Description of this security collection
     */
    public function __construct($name = null, $description = null) {

        $this->setName($name);
        $this->setDescription($description);

    }


    // ----------------------------------------------------- Instance Variables


    /**
     * Description of this web resource collection.
     */
    private $description = null;


    /**
     * The HTTP methods covered by this web resource collection.
     */
    private $methods = array();


    /**
     * The name of this web resource collection.
     */
    private $name = null;


    /**
     * The URL patterns protected by this security collection.
     */
    private $patterns = array();


    // ------------------------------------------------------------- Properties


    /**
     * Return the description of this web resource collection.
     */
    public function getDescription() {

        return ($this->description);

    }


    /**
     * Set the description of this web resource collection.
     *
     * @param description The new description
     */
    public function setDescription($description) {

        $this->description = $description;

    }


    /**
     * Return the name of this web resource collection.
     */
    public function getName() {

        return ($this->name);

    }


    /**
     * Set the name of this web resource collection
     *
     * @param name The new name
     */
    public function setName($name) {

        $this->name = $name;

    }


    // --------------------------------------------------------- Public Methods


    /**
     * Add an HTTP request method to be part of this web resource collection.
     */
    public function addMethod($method) {

        if ($method === null)
            return;
        $this->methods[] = $method;

    }


    /**
     * Add a URL pattern to be part of this web resource collection.
     */
    public function addPattern($pattern) {

        if ($pattern === null)
            return;
        $pattern = urldecode($pattern);
        $this->patterns[] = $pattern; 

    }


    /**
     * Return <code>true</code> if the specified HTTP request method is
     * part of this web resource collection.
     *
     * @param method Request method to check
     */
    public function findMethod($method) {

        if (count($this->methods) == 0)
            return (true);
        return in_array($method, $this->methods);

    }


    /**
     * Return the set of HTTP request methods that are part of this web
     * resource collection, or a zero-length array if all request methods
     * are included.
     */
    public function findMethods() {

        return ($this->methods);

    }


    /**
     * Is the specified pattern part of this web resource collection?
     *
     * @param pattern Pattern to be compared
     */
    public function findPattern($pattern) {

        return in_array($pattern, $this->patterns);

    }


    /**
     * Return the set of URL patterns that are part of this web resource
     * collection.  If none have been specified, a zero-length array is
     * returned.
     */
    public function findPatterns() {

        return ($this->patterns);

    }


    /**
     * Remove the specified HTTP request method from those that are part
     * of this web resource collection.
     *
     * @param method Request method to be removed
     */
    public function removeMethod($method) {

        if ($method === null)
            return;

        for ($i = 0; $i < count($this->methods); $i++) {
            if ($this->methods[$i] == $method) {
                unset($this->methods[$i]);
                return;
            }
        }

    }


    /**
     * Remove the specified URL pattern from those that are part of this
     * web resource collection.
     *
     * @param pattern Pattern to be removed
     */
    public function removePattern($pattern) {

        if (pattern === null)
            return;

        for ($i = 0; $i < count($this->patterns); $i++) {
            if ($this->patterns[$i] == $pattern) {
                unset($this->patterns[$i]);
                return;
            }
        }

    }


    /**
     * Return a $representation of this security collection.
     */
    public function __toString() {

        $sb = "SecurityCollection[";
        $sb .= $this->name;
        if ($this->description !== null) {
            $sb .= ", ";
            $sb .= $this->description;
        }
        $sb .= "]";
        return ($sb);

    }


}
?>